library(gganimate)
## Loading required package: ggplot2
library(readxl)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## -- Attaching packages --------------------------------------------- tidyverse 1.2.1 --
## v tibble 2.1.3 v purrr 0.3.2
## v tidyr 1.0.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(gifski)
library(datapasta)
c2015=read_excel("C:/Users/student/Documents/Fall2019/c2015.xlsx")
y=c2015 %>%
filter_all(~!is.na(.)) %>%
filter_all(~!(.=='Unknown')) %>%
filter_all(~!(.=='Not Rep'))
y=y %>%
filter_all(~!(.==str_detect(.,'Not Rep')))%>%
filter_all(~!(.==str_detect(.,'Unknown')))%>%
filter_all(~!(.=='Not Reported'))
y=y %>%
mutate(TRAV_SP1=str_replace(TRAV_SP," MPH","")) %>%
mutate(TRAV_SP1 = as.numeric(TRAV_SP1)) %>%
mutate(TRAV_SP1 =replace(TRAV_SP1,is.na(TRAV_SP1),mean(TRAV_SP1,na.rm=TRUE)))
## Warning: NAs introduced by coercion
y=y%>%
mutate(AGE = replace(AGE, AGE == "Less than 1" , "0")) %>%
mutate(AGE = as.numeric(AGE))%>%
mutate(AGE = replace(AGE,is.na(AGE),mean(AGE,na.rm=TRUE)))
y=y %>%
filter(SEAT_POS=='Front Seat, Left Side')
table(y$SEX)
##
## Female Male
## 4006 10963
1.Continue with Question 15 of Assignment 5. We want to add a moving/transition variable to the grapth using the gganimate package. Do the follows to achieve that Install and load the gganimate package Group the data by SEX, INJ_SEV and MONTH then calculate the mean travel speed Reuse the code and 15 and adding transition_states(MONTH) to add the MONTH variable Adding a title using labs(title = ‘MONTH = {closest_state}’)
y4=y %>%
group_by(MONTH) %>%
summarise(m_s=mean(TRAV_SP1)) %>%
mutate(center_mean=mean(m_s)) %>%
mutate(sd=m_s-center_mean) %>%
mutate(sd_level=ifelse(sd>0,"above","below"))
ggplot(y4, aes(x=reorder(MONTH,sd), y=sd)) +
geom_bar(stat='identity', aes(fill=sd_level), width=0.5) +
scale_fill_manual(name="Standarized mean speed per month",
labels = c("Above Average", "Below Average"),
values = c("above"="#00ba38", "below"="#f8766d")) +
labs(subtitle="Normalised average speed from 'c2015'",
title= labs(title = 'MONTH = {closest_state}')) +
coord_flip()+
transition_states(MONTH)
a=y %>%
group_by(DRINKING,SEX,MONTH) %>%
summarise(counts=n())
ggplot(a,aes(x=DRINKING,y=counts,fill=SEX))+
geom_col()+
geom_text(aes(label = counts), vjust = -0.3)+
transition_states(MONTH)+
labs(title= labs(title = 'MONTH = {closest_state}'))
3.In this question, we work with the household debt and credit data. Do the follows to import the data to R
Download the data at this link. (Notice that if you use read_excel to read this file, you may not have the desired outcome.) Install the datapasta package so that we can copy and paste the data to R Open the downloaded file, tab Page 3 Data. Name the first column of the data table as Quarter Select and copy the data table including the variables In Rstudio: Tools -> Addins -> Browse Addins -> Select Paste as tribble -> Excute Don’t forget to name the data Plot a line plot between Student.Loan and Credit.Card
b=tibble::tribble(
~Quarter, ~Mortgage, ~HE.Revolving, ~Auto.Loan, ~Credit.Card, ~Student.Loan, ~Other, ~Total,
"03:Q1", 4.94, 0.24, 0.64, 0.69, 0.24, 0.48, 7.23,
"03:Q2", 5.08, 0.26, 0.62, 0.69, 0.24, 0.49, 7.38,
"03:Q3", 5.18, 0.27, 0.68, 0.69, 0.25, 0.48, 7.56,
"03:Q4", 5.66, 0.3, 0.7, 0.7, 0.25, 0.45, 8.07,
"04:Q1", 5.84, 0.33, 0.72, 0.7, 0.26, 0.45, 8.29,
"04:Q2", 5.97, 0.37, 0.74, 0.7, 0.26, 0.42, 8.46,
"04:Q3", 6.21, 0.43, 0.75, 0.71, 0.33, 0.41, 8.83,
"04:Q4", 6.36, 0.47, 0.73, 0.72, 0.35, 0.42, 9.04,
"05:Q1", 6.51, 0.5, 0.73, 0.71, 0.36, 0.39, 9.21,
"05:Q2", 6.7, 0.53, 0.77, 0.72, 0.37, 0.4, 9.49,
"05:Q3", 6.91, 0.54, 0.83, 0.73, 0.38, 0.41, 9.79,
"05:Q4", 7.1, 0.57, 0.79, 0.74, 0.39, 0.42, 10,
"06:Q1", 7.44, 0.58, 0.79, 0.72, 0.43, 0.42, 10.38,
"06:Q2", 7.76, 0.59, 0.8, 0.74, 0.44, 0.42, 10.75,
"06:Q3", 8.05, 0.6, 0.82, 0.75, 0.45, 0.44, 11.11,
"06:Q4", 8.23, 0.6, 0.82, 0.77, 0.48, 0.41, 11.31,
"07:Q1", 8.42, 0.61, 0.79, 0.76, 0.51, 0.4, 11.5,
"07:Q2", 8.71, 0.62, 0.81, 0.8, 0.51, 0.41, 11.85,
"07:Q3", 8.93, 0.63, 0.82, 0.82, 0.53, 0.41, 12.13,
"07:Q4", 9.1, 0.65, 0.82, 0.84, 0.55, 0.42, 12.37,
"08:Q1", 9.23, 0.66, 0.81, 0.84, 0.58, 0.42, 12.54,
"08:Q2", 9.27, 0.68, 0.81, 0.85, 0.59, 0.4, 12.6,
"08:Q3", 9.29, 0.69, 0.81, 0.86, 0.61, 0.41, 12.68,
"08:Q4", 9.26, 0.71, 0.79, 0.87, 0.64, 0.41, 12.67,
"09:Q1", 9.14, 0.71, 0.77, 0.84, 0.66, 0.41, 12.53,
"09:Q2", 9.06, 0.71, 0.74, 0.82, 0.68, 0.39, 12.41,
"09:Q3", 8.94, 0.71, 0.74, 0.81, 0.69, 0.38, 12.28,
"09:Q4", 8.84, 0.71, 0.72, 0.8, 0.72, 0.38, 12.17,
"10:Q1", 8.83, 0.7, 0.7, 0.76, 0.76, 0.36, 12.12,
"10:Q2", 8.7, 0.68, 0.7, 0.74, 0.76, 0.35, 11.94,
"10:Q3", 8.61, 0.67, 0.71, 0.73, 0.78, 0.34, 11.84,
"10:Q4", 8.45, 0.67, 0.71, 0.73, 0.81, 0.34, 11.71,
"11:Q1", 8.54, 0.64, 0.71, 0.7, 0.84, 0.33, 11.75,
"11:Q2", 8.52, 0.62, 0.71, 0.69, 0.85, 0.33, 11.73,
"11:Q3", 8.4, 0.64, 0.73, 0.69, 0.87, 0.33, 11.66,
"11:Q4", 8.27, 0.63, 0.73, 0.7, 0.87, 0.33, 11.54,
"12:Q1", 8.19, 0.61, 0.74, 0.68, 0.9, 0.32, 11.44,
"12:Q2", 8.15, 0.59, 0.75, 0.67, 0.91, 0.31, 11.38,
"12:Q3", 8.03, 0.57, 0.77, 0.67, 0.96, 0.31, 11.31,
"12:Q4", 8.03, 0.56, 0.78, 0.68, 0.97, 0.32, 11.34,
"13:Q1", 7.93, 0.55, 0.79, 0.66, 0.99, 0.31, 11.23,
"13:Q2", 7.84, 0.54, 0.81, 0.67, 0.99, 0.3, 11.15,
"13:Q3", 7.9, 0.54, 0.85, 0.67, 1.03, 0.3, 11.28,
"13:Q4", 8.05, 0.53, 0.86, 0.68, 1.08, 0.32, 11.52,
"14:Q1", 8.17, 0.53, 0.88, 0.66, 1.11, 0.31, 11.65,
"14:Q2", 8.1, 0.52, 0.91, 0.67, 1.12, 0.32, 11.63,
"14:Q3", 8.13, 0.51, 0.93, 0.68, 1.13, 0.33, 11.71,
"14:Q4", 8.17, 0.51, 0.96, 0.7, 1.16, 0.34, 11.83,
"15:Q1", 8.17, 0.51, 0.97, 0.68, 1.19, 0.33, 11.85,
"15:Q2", 8.12, 0.5, 1.01, 0.7, 1.19, 0.34, 11.85,
"15:Q3", 8.26, 0.49, 1.05, 0.71, 1.2, 0.35, 12.07,
"15:Q4", 8.25, 0.49, 1.06, 0.73, 1.23, 0.35, 12.12,
"16:Q1", 8.37, 0.49, 1.07, 0.71, 1.26, 0.35, 12.25,
"16:Q2", 8.36, 0.48, 1.1, 0.73, 1.26, 0.36, 12.29,
"16:Q3", 8.35, 0.47, 1.14, 0.75, 1.28, 0.37, 12.35,
"16:Q4", 8.48, 0.47, 1.16, 0.78, 1.31, 0.38, 12.58,
"17:Q1", 8.63, 0.46, 1.17, 0.76, 1.34, 0.37, 12.73,
"17:Q2", 8.69, 0.45, 1.19, 0.78, 1.34, 0.38, 12.84,
"17:Q3", 8.74, 0.45, 1.21, 0.81, 1.36, 0.39, 12.96,
"17:Q4", 8.88, 0.44, 1.22, 0.83, 1.38, 0.39, 13.15,
"18:Q1", 8.94, 0.44, 1.23, 0.82, 1.41, 0.39, 13.21,
"18:Q2", 9, 0.43, 1.24, 0.83, 1.41, 0.39, 13.29,
"18:Q3", 9.14, 0.42, 1.27, 0.84, 1.44, 0.4, 13.51,
"18:Q4", 9.12, 0.41, 1.27, 0.87, 1.46, 0.41, 13.54,
"19:Q1", 9.24, 0.41, 1.28, 0.85, 1.49, 0.4, 13.67,
"19:Q2", 9.41, 0.4, 1.3, 0.87, 1.48, 0.41, 13.86
)
ggplot(b,aes(Student.Loan,Credit.Card))+
geom_line()
4.We want to add a moving variable in the graph of 3. The function transition_reveal (link) is great for this. You may tempt to add transition_reveal(Quarter) but notice that transition_reveal does not take the current form of Quarter. Hint: You can create a dummy variable running from 1 to the size of the data and make it the transition variable.
b$qt = seq.Date(as.Date('2003-01-01'),as.Date('2019-04-01'), by = '3 months')
ggplot(b,aes(Student.Loan,Credit.Card))+
geom_line()+
geom_segment(aes(xend = 25, yend = Credit.Card)) +
geom_point() +
geom_text(aes(x = 31.1, label = qt)) +
transition_reveal(qt)+
labs(subtitle="Student loan vs. Credit card through date")
5.The Quarter variable is not in the right format (date). Create the date column where the date is the first day of each quarter. Plot the graph of Student.Loan by date. Hint Use: the seq.date functions with the increment being three months.
b$Date = seq.Date(as.Date('2003-01-01'),as.Date('2019-04-01'), by = '3 months')
ggplot(b,aes(x=Date,y=Student.Loan))+
geom_line()
6.Add transition_reveal(date) to the plot in Question 5. to reveal the graph by quarters.
b$Date = seq.Date(as.Date('2003-01-01'),as.Date('2019-04-01'), by = '3 months')
ggplot(b,aes(x=Date,y=Student.Loan))+
geom_line()+
geom_text(aes(label = Date))+
transition_reveal(Date)
7.Use geom_point and geom_text to plot the moving point and the value of the moving points. Hint: geom_point()+ geom_text(aes(label=Student.Loan)) should work.
b$Date = seq.Date(as.Date('2003-01-01'),as.Date('2019-04-01'), by = '3 months')
ggplot(b,aes(Date,Student.Loan))+
geom_point()+
transition_reveal(Date)+
geom_text(aes(label=Student.Loan))
8.Include the graphs of other debts to the plot in Question 7, revealing them by date/quarter and differentiating them by colors. Hint: you may want to change the data from long to wide using the gather function.
b2=b %>%
gather(Debt, key=KIND_OF_DEBT,Student.Loan,Other)
b2$Date = seq.Date(as.Date('2003-01-01'),as.Date('2019-04-01'), by = '3 months')
ggplot(b2,aes(Date,Debt,color=KIND_OF_DEBT))+
geom_point(size=5)+
transition_reveal(Date)+
geom_text(aes(label=Debt),color="black")
9.What is the debt that most correlated with the Total debt. Plot the graph of this debt and the total together revealing by years, differentiation by colors. Plot the remaining debt together in another plot, revealing by years, differentiating by colors. Give a comment on the plots. Label and put captions to the plots.
cor.test (b$Auto.Loan,b$Total,method="pearson")
##
## Pearson's product-moment correlation
##
## data: b$Auto.Loan and b$Total
## t = 6.7793, df = 64, p-value = 4.531e-09
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.4795003 0.7683141
## sample estimates:
## cor
## 0.6465033
cor.test(b$Credit.Card,b$Total,method="pearson")
##
## Pearson's product-moment correlation
##
## data: b$Credit.Card and b$Total
## t = 5.8555, df = 64, p-value = 1.787e-07
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.4067444 0.7285211
## sample estimates:
## cor
## 0.5906321
cor.test(b$Student.Loan,b$Total,method="pearson")
##
## Pearson's product-moment correlation
##
## data: b$Student.Loan and b$Total
## t = 9.5554, df = 64, p-value = 6.134e-14
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6442866 0.8508800
## sample estimates:
## cor
## 0.766753
cor.test(b$Other,b$Total,method="pearson")
##
## Pearson's product-moment correlation
##
## data: b$Other and b$Total
## t = -3.6182, df = 64, p-value = 0.0005861
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.5947939 -0.1888916
## sample estimates:
## cor
## -0.4120847
cor.test(b$Mortgage ,b$Total,method="pearson")
##
## Pearson's product-moment correlation
##
## data: b$Mortgage and b$Total
## t = 33.61, df = 64, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.9558488 0.9833261
## sample estimates:
## cor
## 0.9728223
cor.test(b$HE.Revolving ,b$Total,method="pearson")
##
## Pearson's product-moment correlation
##
## data: b$HE.Revolving and b$Total
## t = 3.9167, df = 64, p-value = 0.0002206
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2212322 0.6161753
## sample estimates:
## cor
## 0.43972
#Mortgage loan is the most correlated with Total debt
b3=b %>%
gather(Debt, key=KIND_OF_DEBT,Mortgage,Total)
b3$Date = seq.Date(as.Date('2003-01-01'),as.Date('2019-04-01'), by = '3 months')
format(as.Date(b3$Date, format="%d/%m/%Y"),"%Y")
## [1] "2003" "2003" "2003" "2003" "2004" "2004" "2004" "2004" "2005" "2005"
## [11] "2005" "2005" "2006" "2006" "2006" "2006" "2007" "2007" "2007" "2007"
## [21] "2008" "2008" "2008" "2008" "2009" "2009" "2009" "2009" "2010" "2010"
## [31] "2010" "2010" "2011" "2011" "2011" "2011" "2012" "2012" "2012" "2012"
## [41] "2013" "2013" "2013" "2013" "2014" "2014" "2014" "2014" "2015" "2015"
## [51] "2015" "2015" "2016" "2016" "2016" "2016" "2017" "2017" "2017" "2017"
## [61] "2018" "2018" "2018" "2018" "2019" "2019" "2003" "2003" "2003" "2003"
## [71] "2004" "2004" "2004" "2004" "2005" "2005" "2005" "2005" "2006" "2006"
## [81] "2006" "2006" "2007" "2007" "2007" "2007" "2008" "2008" "2008" "2008"
## [91] "2009" "2009" "2009" "2009" "2010" "2010" "2010" "2010" "2011" "2011"
## [101] "2011" "2011" "2012" "2012" "2012" "2012" "2013" "2013" "2013" "2013"
## [111] "2014" "2014" "2014" "2014" "2015" "2015" "2015" "2015" "2016" "2016"
## [121] "2016" "2016" "2017" "2017" "2017" "2017" "2018" "2018" "2018" "2018"
## [131] "2019" "2019"
ggplot(b3,aes(Date,Debt,color=KIND_OF_DEBT))+
geom_point(size=5)+
transition_reveal(Date)+
labs(title= "Mortgage vs Total loan")
10.Use transition_reveal and transition_states to explore the data of the U.S Economy. Plot three animated plots.
A=tibble::tribble(
~Quarter, ~Date, ~gdph, ~gdppothq, ~difa,
"Q1-2000", "Mar-2000", 12.924, 12.722, "1.46%",
"Q2-2000", "Jun-2000", 13.161, 12.854, "7.53%",
"Q3-2000", "Sep-2000", 13.178, 12.984, "0.54%",
"Q4-2000", "Dec-2000", 13.261, 13.111, "2.52%",
"Q1-2001", "Mar-2001", 13.223, 13.233, "-1.14%",
"Q2-2001", "Jun-2001", 13.3, 13.348, "2.36%",
"Q3-2001", "Sep-2001", 13.245, 13.46, "-1.65%",
"Q4-2001", "Dec-2001", 13.281, 13.566, "1.09%",
"Q1-2002", "Mar-2002", 13.397, 13.668, "3.54%",
"Q2-2002", "Jun-2002", 13.478, 13.765, "2.45%",
"Q3-2002", "Sep-2002", 13.538, 13.859, "1.79%",
"Q4-2002", "Dec-2002", 13.559, 13.951, "0.62%",
"Q1-2003", "Mar-2003", 13.634, 14.045, "2.24%",
"Q2-2003", "Jun-2003", 13.752, 14.136, "3.48%",
"Q3-2003", "Sep-2003", 13.985, 14.228, "6.97%",
"Q4-2003", "Dec-2003", 14.146, 14.321, "4.67%",
"Q1-2004", "Mar-2004", 14.221, 14.415, "2.15%",
"Q2-2004", "Jun-2004", 14.33, 14.513, "3.08%",
"Q3-2004", "Sep-2004", 14.465, 14.611, "3.84%",
"Q4-2004", "Dec-2004", 14.61, 14.71, "4.07%",
"Q1-2005", "Mar-2005", 14.772, 14.807, "4.50%",
"Q2-2005", "Jun-2005", 14.84, 14.902, "1.86%",
"Q3-2005", "Sep-2005", 14.972, 14.994, "3.61%",
"Q4-2005", "Dec-2005", 15.067, 15.084, "2.55%",
"Q1-2006", "Mar-2006", 15.267, 15.167, "5.43%",
"Q2-2006", "Jun-2006", 15.303, 15.245, "0.94%",
"Q3-2006", "Sep-2006", 15.326, 15.321, "0.62%",
"Q4-2006", "Dec-2006", 15.457, 15.395, "3.45%",
"Q1-2007", "Mar-2007", 15.493, 15.469, "0.95%",
"Q2-2007", "Jun-2007", 15.582, 15.543, "2.31%",
"Q3-2007", "Sep-2007", 15.667, 15.617, "2.19%",
"Q4-2007", "Dec-2007", 15.762, 15.69, "2.46%",
"Q1-2008", "Mar-2008", 15.671, 15.762, "-2.28%",
"Q2-2008", "Jun-2008", 15.752, 15.835, "2.08%",
"Q3-2008", "Sep-2008", 15.667, 15.904, "-2.15%",
"Q4-2008", "Dec-2008", 15.328, 15.971, "-8.38%",
"Q1-2009", "Mar-2009", 15.156, 16.032, "-4.42%",
"Q2-2009", "Jun-2009", 15.134, 16.084, "-0.57%",
"Q3-2009", "Sep-2009", 15.189, 16.132, "1.46%",
"Q4-2009", "Dec-2009", 15.356, 16.176, "4.47%",
"Q1-2010", "Mar-2010", 15.415, 16.218, "1.55%",
"Q2-2010", "Jun-2010", 15.557, 16.258, "3.74%",
"Q3-2010", "Sep-2010", 15.672, 16.3, "2.98%",
"Q4-2010", "Dec-2010", 15.751, 16.343, "2.02%",
"Q1-2011", "Mar-2011", 15.713, 16.392, "-0.96%",
"Q2-2011", "Jun-2011", 15.825, 16.444, "2.89%",
"Q3-2011", "Sep-2011", 15.821, 16.499, "-0.11%",
"Q4-2011", "Dec-2011", 16.004, 16.556, "4.72%",
"Q1-2012", "Mar-2012", 16.129, 16.615, "3.17%",
"Q2-2012", "Jun-2012", 16.199, 16.678, "1.73%",
"Q3-2012", "Sep-2012", 16.221, 16.743, "0.54%",
"Q4-2012", "Dec-2012", 16.239, 16.811, "0.45%",
"Q1-2013", "Mar-2013", 16.383, 16.88, "3.59%",
"Q2-2013", "Jun-2013", 16.403, 16.951, "0.49%",
"Q3-2013", "Sep-2013", 16.532, 17.023, "3.17%",
"Q4-2013", "Dec-2013", 16.664, 17.096, "3.23%",
"Q1-2014", "Mar-2014", 16.617, 17.168, "-1.13%",
"Q2-2014", "Jun-2014", 16.842, 17.242, "5.53%",
"Q3-2014", "Sep-2014", 17.047, 17.316, "4.97%",
"Q4-2014", "Dec-2014", 17.143, 17.392, "2.27%",
"Q1-2015", "Mar-2015", 17.278, 17.468, "3.18%",
"Q2-2015", "Jun-2015", 17.406, 17.546, "3.00%",
"Q3-2015", "Sep-2015", 17.463, 17.624, "1.33%",
"Q4-2015", "Dec-2015", 17.469, 17.702, "0.13%",
"Q1-2016", "Mar-2016", 17.557, 17.779, "2.03%",
"Q2-2016", "Jun-2016", 17.639, 17.855, "1.90%",
"Q3-2016", "Sep-2016", 17.735, 17.93, "2.19%",
"Q4-2016", "Dec-2016", 17.824, 18.006, "2.02%",
"Q1-2017", "Mar-2017", 17.925, 18.081, "2.29%",
"Q2-2017", "Jun-2017", 18.021, 18.158, "2.15%",
"Q3-2017", "Sep-2017", 18.164, 18.236, "3.20%",
"Q4-2017", "Dec-2017", 18.323, 18.316, "3.55%",
"Q1-2018", "Mar-2018", 18.438, 18.401, "2.55%",
"Q2-2018", "Jun-2018", 18.598, 18.489, "3.51%",
"Q3-2018", "Sep-2018", 18.733, 18.58, "2.93%",
"Q4-2018", "Dec-2018", 18.784, 18.673, "1.09%",
"Q1-2019", "Mar-2019", 18.927, 18.768, "3.10%",
"Q2-2019", "Jun-2019", 19.022, 18.866, "2.01%",
"Q3-2019", "Sep-2019", 19.113, 18.964, "1.92%"
)
A2=A %>%
mutate(date=seq.Date(as.Date('2000-03-01'),as.Date('2019-09-01'), by = '3 months')) %>%
gather(GDP, key=GPD_TYPE,gdph,gdppothq)
ggplot(A2,aes(date,GDP,color=GPD_TYPE))+
geom_point(size=5)+
geom_line()+
transition_reveal(date)+
labs(title= "gdph vs gdppothq")
A3=A2 %>%
mutate(difa=str_replace(difa,"%","")) %>%
mutate(difa=as.numeric(difa))
ggplot(A3,aes(date,difa))+
geom_point()+
geom_line()+
transition_reveal(date)
B=A %>%
mutate(date=seq.Date(as.Date('2000-03-01'),as.Date('2019-09-01'), by = '3 months')) %>%
mutate(MONTH=format(as.Date(date, format="%Y/%m/%d"),"%m")) %>%
group_by(MONTH) %>%
summarise(mean_gdp=mean(gdph))
ggplot(B,aes(MONTH,mean_gdp))+
geom_col()+
transition_states(MONTH)+
geom_text(aes(label=mean_gdp),vjust = -0.3)